package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.InGoods;
import pers.cj.pdos.entity.PayOrder;
import pers.cj.pdos.service.IPayOrderService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.InGoodsVo;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/funds")
public class PayOrderController extends BaseController {
    @Autowired
    IPayOrderService payOrderService;

    @RequiresPermissions("payOrder:pay")
    @RequestMapping("/payOrderNew")
    public String payOrderNew(Model model, String providerId) {
        if (!Utils.isEmpty(providerId)) {
            model.addAttribute("providerId", providerId);
        }
        return "funds/payOrderNew";
    }


    @RequiresPermissions("payOrder:pay")
    @RequestMapping("/payOrderDetail")
    public String payOrderDetail(Model model, String id) {
        model.addAttribute("id",id);
        return "funds/payOrderDetail";
    }

    @ResponseBody
    @RequiresPermissions("payOrder:pay")
    @RequestMapping("/payOrder/add")
    public ResponseEntity add(@RequestBody PayOrder payOrder){
        Company company = getCompany();
        if (Utils.isEmpty(payOrder.getNo())){
            return ResponseEntity.fail("编号不能为空");
        }

        if (payOrder.getDate() == null){
            return ResponseEntity.fail("业务日期不能为空");
        }

        if (Utils.isEmpty(payOrder.getUserId())){
            return ResponseEntity.fail("经办人不能为空");
        }

        if (Utils.isEmpty(payOrder.getSettAccountId())){
            return ResponseEntity.fail("结算账户不能为空");
        }

        if (Utils.isEmpty(payOrder.getProviderId())){
            return ResponseEntity.fail("供应商不能为空");
        }

        if (payOrderService.checkNo(payOrder.getNo(),company.getId())){
            return ResponseEntity.fail("编号已被使用");
        }

        DataUtil.processEntity(company,payOrder);
        payOrder.setMakeTime(new Date());
        payOrder.setMakeUserId(getLoginUserId());
        payOrder.setClose(false);
        payOrderService.insert(payOrder);

        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("payOrder:close")
    @RequestMapping("/payOrder/close")
    public ResponseEntity close(@RequestBody PayOrder payOrder){
        //检查是否已经关闭
        if (payOrderService.checkClose(payOrder)){
            return ResponseEntity.fail("该单据已被关闭，不能重复关闭");
        }

        payOrderService.close(payOrder);

        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/payOrder/getVoById")
    public ResponseData getVoById(String id){
        return ResponseData.success(payOrderService.getVoById(id));
    }


    @ResponseBody
    @RequiresPermissions("payOrder:pay")
    @RequestMapping("/payOrder/updateImage")
    public ResponseEntity updateImage(@RequestBody PayOrder payOrder){
        try {
            PayOrder temp = new PayOrder();
            temp.setId(payOrder.getId());
            temp.setImage(payOrder.getImage());
            payOrderService.updateById(temp);
            return ResponseEntity.success();
        }catch (Exception e) {
            return ResponseEntity.fail("更新图片失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/payOrder/uploadImg", method = RequestMethod.POST)
    public ResponseData uploadImg(MultipartFile imageFile) {
        return processUploadImg(imageFile, "payOrder");
    }
}
